/*
 * Created empty Manuscript class so Conference class
 * would compile.
 */
package conference;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * The Review class for the Smart Peer Review program.
 * @author Katie Park and Allison Graham
 * @version 1.0
 */
public class Review extends AbstractDocument {
	//Class Variables
	/**
	 * States if the document is submitted.
	 */
	private boolean isDone;
	
	/**
	 * The file from with the file contents might have been taken from.
	 */
	private File myFile;
	
	//Constructors
	/**
	 * Creates a Review object given a title, file, and Reviewer.
	 * -Precondition: none.
	 * -Postcondition: super.getTitle() == theTitle.
	 * -Postcondition: super.getFile() == theFile.
	 * -Postcondition: super.getReviewer == theReviewer.
	 * -Postcondition: isDone == true.
	 * @param theTitle The title of this Review.
	 * @param theFile The File containing the Text of this Review.
	 * @param theReviewer The Reviewer who created this review.
	 */
	public Review(final String theTitle, final File theFile, 
			final Reviewer theReviewer) {
		super(theTitle, getMyFileContents(theFile), theReviewer);
		isDone = true;
		myFile = theFile;
	}
	
	/**
	 * Creats a Review Object given a title, fileContents and Reviewer.
	 * -Precondition: none.
	 * -Postcondition: super.getTitle() == theTitle.
	 * -Postcondition: super.getFile() == theFile.
	 * -Postcondition: super.getReviewer == theReviewer.
	 * -Postcondition: isDone == true.
	 * @param theTitle The title of this Review.
	 * @param theFileContents The string containing the Text of this Review.
	 * @param theReviewer The Reviewer who created this review.
	 */
	public Review(final String theTitle, final String theFileContents, 
			final Reviewer theReviewer) {
		super(theTitle, theFileContents, theReviewer);
		if(theFileContents.equals("")){
			isDone = false;
		}
		else {
			isDone = true;
		}
		myFile = null;
	}
	
	/**
	 * Creates a review object given a reviewer.
	 *  
	 * @param theReviewer creator of this reviewer.
	 */
	public Review(final Reviewer theReviewer){
		super(theReviewer);
		isDone = false;
		myFile = null;
	}
	
	//Methods
	
	/**
	 * Returns is text is submitted for this review.
	 * @return If text has been submitted for this review as boolean.
	 */
	public boolean isComplete() {
		return isDone;
	}
	
	/**
	 * Returns the reviewer assigned for this review.
	 * @return The reviewer assigned for this review as Reviewer.
	 * @
	 */
	public Reviewer getReviewer() {
		return (Reviewer) super.getCreator();
	} 
	
	/**
	 * Returns the File that contains the text of the Review.
	 * @return The File as File, or null if not existing.
	 */
	public File getFile() {
		return myFile;
	}
	
    /**
     * Returns a string representation of the file contained within 
     * this document
     * @return The file contents in this document as String.
     * @throws FileNotFoundException 
     */
    private final static String getMyFileContents(final File theFile){
    	 StringBuilder contents = new StringBuilder();    
    	 try {
    		 //use buffering, reading one line at a time
    		 //FileReader always assumes default encoding is OK!
    		 BufferedReader input =  new BufferedReader(new FileReader(theFile));
    	     try {
    	    	 String line = null; //not declared within while loop
    	    	 /*
    	    	  * readLine is a bit quirky :
    	    	  * it returns the content of a line MINUS the newline.
    	    	  * it returns null only for the END of the stream.
    	    	  * it returns an empty String if two newlines appear in a row.
    	    	  */
    	    	 while (( line = input.readLine()) != null){
    	    		 contents.append(line);
    	    		 contents.append(System.getProperty("line.separator"));
    	    	 }
    	     }
    	     finally {
    	    	 input.close();
    	     }
    	 }
    	 catch (IOException ex){
    		 ex.printStackTrace();
    	 }
    	 
    	 return contents.toString();
    }
}